package com.company.management.service.reward.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.company.management.domain.reward.Department;
import com.company.management.domain.reward.TalentUser;
import com.company.management.domain.reward.vo.DepartmentVO;
import com.company.management.domain.reward.vo.UserVO;
import com.company.management.mapper.reward.DepartmentMapper;
import com.company.management.service.reward.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
 
    @Autowired
    private DepartmentMapper departmentMapper;
 
    public List<DepartmentVO> getAllDepartmentsWithUsers(Integer userId) {
        List<Department> departments = departmentMapper.getAllDepartments();
        return departments.stream().map(department -> {
            DepartmentVO departmentVO = new DepartmentVO();
            departmentVO.setId(department.getDeptId());
            departmentVO.setName(department.getDeptName());
            List<TalentUser> users = departmentMapper.getUsersByDepartmentId(department.getDeptId(),userId);
            if(CollectionUtil.isEmpty(users)){
                return null;
            }
            departmentVO.setChildren(users.stream().map(user -> {
                UserVO userVO = new UserVO();
                userVO.setId(user.getUserId());
                userVO.setName(user.getUserName());
                return userVO;
            }).collect(Collectors.toList()));
            return departmentVO;
        }) .filter(Objects::nonNull).collect(Collectors.toList());
    }
}